from etx import db


class BaseService(object):

    @staticmethod
    def transactional(func):
        def wrapper(*args, **kwargs):
            try:
                db.session.begin(subtransactions=True)
                res = func(*args, **kwargs)
                db.session.commit()
                return res
            except Exception as e:
                print(e)
                db.session.rollback()
            finally:
                if db.session is not None:
                    db.session.close()

        return wrapper
